﻿using Dapper;
using SetSailModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SetSailDal
{
    public class CDDJLDal
    {

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public static ResultModel<List<QH_CDDJL>> GetPageList(int pageIndex, int pageSize, string where, string sort = "CJSJ")
        {
            ResultModel<List<QH_CDDJL>> result = new ResultModel<List<QH_CDDJL>>();
            try
            {
                int skip = 1;//从第几条开始
                int take = pageIndex * pageSize;//到第几条结束
                if (pageIndex > 0)
                {
                    skip = ((pageIndex - 1) * pageSize) + 1;
                }
                where = where.Length > 0 ? where + " AND SCBJ=0" : " SCBJ=0";
                //string sql = string.Format(@"SELECT *,'删除' btn FROM [dbo].[QH_ZDGL] WHERE SCBJ=0");
                string sql = string.Format($@"SELECT * FROM
                            (SELECT ROW_NUMBER() OVER(ORDER BY {sort} DESC) AS RowNum,*,'删除' btn  FROM  [dbo].[QH_CDDJL] with (nolock) WHERE {where}) AS result
                            WHERE  RowNum >= {skip}   AND RowNum <= {take} ORDER BY {sort} DESC");
                string sql_count = $"SELECT COUNT(1) FROM [dbo].[QH_CDDJL] with (nolock) where {where}";
                using (DBHelper help = new DBHelper())
                {
                    result.Data = help.DbContext.Query<QH_CDDJL>(sql) as List<QH_CDDJL>;
                    result.Count = (int)help.DbContext.ExecuteScalar(sql_count);
                }
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.Fail;
                result.Message = ex.Message;
            }
            return result;
        }
    }
}
